package com.pig4cloud.pigx.common.customize.exception;

import lombok.Data;
import org.apache.logging.log4j.util.Strings;

/**
 * API请求异常
 *
 * @since 2022/11/30 17:55
 */
@Data
public class ApiRequestException extends Exception {

	/**
	 * 错误码
	 */
	private String code;

	/**
	 * Constructs a new exception with {@code null} as its detail message.
	 * The cause is not initialized, and may subsequently be initialized by a
	 * call to {@link #initCause}.
	 */
	public ApiRequestException() {
	}

	/**
	 * Constructs a new exception with the specified detail message.  The
	 * cause is not initialized, and may subsequently be initialized by
	 * a call to {@link #initCause}.
	 *
	 * @param message the detail message. The detail message is saved for
	 *                later retrieval by the {@link #getMessage()} method.
	 */
	public ApiRequestException(String message) {
		super(message);
	}

	public ApiRequestException(String code, String message) {
		super(message);
		this.code = code;
	}

	@Override
	public String getMessage() {
		if (Strings.isBlank(code)) {
			return super.getMessage();
		}

		return this.code + " : " + super.getMessage();
	}

}
